<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/2/26
 * Time: 下午4:42
 */
class Bill extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation');//加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->load->model('user_model');
        $this->load->model('customer_model');
        $this->load->model('order_model');
        $this->load->model('bill_model');
        $this->load->model('goods_model');
        $this->load->library('email');
    }


    // 确认期付款申请页面
    public function insure_account_period_repayment_apply()
    {
        if (isset($_GET['apply_id'])) {
            $apply_id = decrypt($_GET['apply_id']);
            if (is_int($apply_id) && $apply_id > 0) {

                $data['apply_id'] = $apply_id;
                $this->load->view('bill/insure_account_period_repayment_apply', $data);
            } else {
                $this->load->view('error');
            }
        } else {
            $this->load->view('error');
        }
    }

    //User申请提高账期额度
    public function account_admin()
    {
        $customer_id = $this->session->customer_id;
        $query = $this->db->query("SELECT identity ,parent_id ,id  FROM app_customer WHERE id=" . $customer_id);
        $r_info = $query->row_array();
        if ($r_info['identity'] == 1) {
            $admin = $this->db->select('*')->where('customer_id', $customer_id)->from('app_admin')->get();
            $account = $admin->row_array();
            if ($_POST) {
                $data_add['current_account_period_quota'] = $account['account_period_quota'];
                $data_add['expect_account_period_quota'] = $_POST['money'];
                $data_add['remark'] = $_POST['other'];
                $data_add['customer_id'] = $customer_id;
                $data_add['status'] = 0;
                $data_add['created_at'] = date('Y-m-d H:i:s', time());
                $query = $this->db->insert('app_adjust_account_period_quota', $data_add);
                if ($query == true) {
                    send_json(array('code' => 0, 'data' => '', 'msg' => '提交申请额度成功'));
                } else {
                    send_json(array('code' => -1, 'data' => '', 'msg' => "提交申请额度失败"));
                }
            }
            $data['account'] = $account;
            $this->load->view('bill/account_admin', $data);
        } else {
            $this->load->view('bill/no_permission_index');
        }

    }

    //Manager申请提高账期额度
    public function account_manage()
    {
        $customer_id = $this->session->customer_id;
        $query = $this->db->query("SELECT identity ,parent_id ,id ,username  FROM app_customer WHERE id=" . $customer_id);
        $r_info = $query->row_array();
        if ($r_info['identity'] == 2) {
            $datas['receiver_customer_id'] = $r_info['parent_id'];
        }
        if ($r_info['identity'] == 3) {
            $query = $this->db->query("SELECT parent_id FROM app_customer WHERE id=" . $r_info['parent_id']);
            $query = $query->row_array();
            if ($query && $query['parent_id']) {
                $datas['receiver_customer_id'] = $query['parent_id'];
            } else {
                $datas['receiver_customer_id'] = $r_info['parent_id'];
            }
        }
        $datas['title'] = '提高账期额度申请';
        $datas['created_at'] = date('Y-m-d H:i:s', time());
        $datas['content'] = $r_info['username'] . "的订单金额超出账期可用额度,请及时申请提高账期额度至后台审核哦";
        $datas['send_customer_id'] = $customer_id;
        $datas['read_status'] = 0;
        $history = $this->db->insert('message_history', $datas);
        if ($history == true) {
            redirect('order/common_order');
        }
    }

    //账单页面
    public function bill_index()
    {
        if (!isset($_GET['bill_id']) || !$_GET['bill_id']) {
            exit('没有此账单');
        }

//        $customer_id = $this->session->customer_id;
        $bill_id = $_GET['bill_id'];
        $bill_customer = $this->db->select('customer_id')->where('id', $bill_id)->from('app_bill')->get()->row_array();
        if (!$bill_customer) {
            exit('账单错误');
        }

        $customer_id = $bill_customer['customer_id'];

        $type = $this->db->select('type')->where('id', $bill_id)->where('customer_id', $customer_id)->from('app_bill')->get()->row_array();
        if ($type['type'] == 1) {
            $sql = "select * from app_bill join app_bill_month on app_bill.id = app_bill_month.bill_id WHERE customer_id='$customer_id' AND app_bill.id = $bill_id";
            $bill = $this->db->query($sql)->row_array();

            $customer = $this->customer_model->query_customer_info($customer_id);
            $bills = array();
            if (isset($bill)) {
                $billss = $this->db->where('bill_id', $bill['bill_id'])->from('app_bill_invoice')->get();
                $bills = $billss->row_array();
            }


            $order = $this->db->select('b.*,o.order_bn,o.money,o.created_at,c.username,c.tel,c.name as cname,ce.name as cename')
                ->where('bill_id', $bill_id)->from('app_order_bill b')
                ->join('app_order o', 'o.id=b.order_id')
                ->join('app_customer c', 'c.id=o.customer_id')
                ->join('app_cost_center ce', 'ce.id=c.cost_center_id', 'left')
                ->get();
            $bills['cata'] = $order->result_array();


            $data['customer'] = $customer;
            $data['bill'] = $bill;
            $data['bills'] = $bills;
            $data['type'] = $type;
            $this->load->view('bill/bill_index', $data);
        } else {
            exit('账单错误');
        }


    }
}